package com.wt;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import android.widget.AdapterView.OnItemClickListener;

import com.wt.R;

public class WtDialog extends Dialog implements DialogInterface {
	private WtParams mWtParams;
	private Window mWindow;
	private ViewGroup mContentView;
	
    public WtDialog(Context context) {
        super(context, R.style.WtDialogTheme);
        mWindow = getWindow();        
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	mWindow.setContentView(R.layout.wt_dialog);
    	
    	View titleBar = mWindow.findViewById(R.id.wt_title_bar);
    	titleBar.setBackgroundColor(mWtParams.mBarBgColor);
    	View dialogContent = mWindow.findViewById(R.id.wt_dialog_content);
    	dialogContent.setBackgroundColor(mWtParams.mContentBgColor);
    	
    	mWtParams.mTitleView = (TextView)mWindow.findViewById(R.id.wt_title_view);
    	if(mWtParams.mTitle != null && mWtParams.mTitleView != null){
			mWtParams.mTitleView.setText(mWtParams.mTitle);
		}
    	
    	mContentView = (ViewGroup)mWindow.findViewById(R.id.wt_dialog_content);
    	    	
    	if(mWtParams.mListView != null){    		
    		mContentView.addView(mWtParams.mListView,new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
    		mWtParams.mListView.setAdapter(mWtParams.mAdapter);
    	}
    }
    
    public void setWtParams(WtParams params){
    	mWtParams = params;
	mWtParams.mDialogInterface = this;
    }
    
    public static class WtParams {
	public DialogInterface mDialogInterface;
    	public CharSequence mTitle;
    	public CharSequence[] mItems;
	public boolean[] mCheckedItems;
	public boolean mIsMultiChoice;
    	public int mContentBgColor;
    	public int mBarBgColor;
	public DialogInterface.OnMultiChoiceClickListener mOnCheckboxClickListener;
		
    	public ListAdapter mAdapter;
	public DialogInterface.OnClickListener mOnClickListener;
    	public AdapterView.OnItemSelectedListener mOnItemSelectedListener;
    	public ListView mListView;
    	public TextView mTitleView;
    }
    
    public static class Builder{
    	private Context mContext;
    	private LayoutInflater mInflater;
    	private WtParams mWtParams;
    	
    	public Builder(Context context){
    		mContext = context;
    		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    	
    		mWtParams = new WtParams();
    	}
    	
    	public Builder setTitle(int titleId){
    		mWtParams.mTitle = mContext.getText(titleId);
    		return this;
    	}
    	
    	public Builder setItems(int itemsId, final OnClickListener listener) {
    		mWtParams.mItems = mContext.getResources().getTextArray(itemsId);
		mWtParams.mOnClickListener = listener;
    		return this;
    	}

    	public Builder setItems(CharSequence[] items, final OnClickListener listener) {
    		mWtParams.mItems = items;
		mWtParams.mOnClickListener = listener;
    		return this;
    	}
    	
    	public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) {
    		mWtParams.mOnItemSelectedListener = listener;
    		return this;
    	}

        public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener) {
		mWtParams.mItems = items;
		mWtParams.mOnCheckboxClickListener = listener;
		mWtParams.mCheckedItems = checkedItems;
		mWtParams.mIsMultiChoice = true;
		return this;
        }
    	
    	public Builder setBarBgColor(int colorId){
    		mWtParams.mBarBgColor = mContext.getResources().getColor(colorId);
    		return this;
    	}
    	
    	public Builder setContentBgColor(int colorId){
    		mWtParams.mContentBgColor = mContext.getResources().getColor(colorId);
    		return this;
    	}

    	public Builder setBarBgColorValue(int color){
    		mWtParams.mBarBgColor = color;
    		return this;
    	}
    	
    	public Builder setContentBgColorValue(int color){
    		mWtParams.mContentBgColor = color;
    		return this;
    	}
    			
    	public WtDialog create() {
    		final WtDialog dialog = new WtDialog(mContext);
    		dialog.setWtParams(mWtParams);
    		apply();
    		return dialog;
    	}
    	
    	private void apply(){
    		if(mWtParams.mTitle != null && mWtParams.mTitleView != null){
    			mWtParams.mTitleView.setText(mWtParams.mTitle);
    		}
    		if(mWtParams.mItems != null){
    			createListView();
    		}
    	}
    	
    	private void createListView(){
    		final RecycleListView listView = (RecycleListView)
                    mInflater.inflate(R.layout.wt_select_dialog, null);
		if(mWtParams.mIsMultiChoice){
			mWtParams.mAdapter = new ArrayAdapter<CharSequence>(mContext, R.layout.wt_select_dialog_multichoice, R.id.text1, mWtParams.mItems) {
				@Override
				public View getView(int position, View convertView, ViewGroup parent) {
					View view = super.getView(position, convertView, parent);
					if (mWtParams.mCheckedItems != null) {
						boolean isItemChecked = mWtParams.mCheckedItems[position];
						if (isItemChecked) {
							listView.setItemChecked(position, true);
						}
					}
					return view;
				}
			};
		}else{
    			mWtParams.mAdapter = new ArrayAdapter<CharSequence>(mContext, R.layout.wt_select_dialog_item, R.id.text1, mWtParams.mItems);
		}
		
		if(mWtParams.mOnClickListener != null){
			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
					mWtParams.mOnClickListener.onClick(mWtParams.mDialogInterface, position);
					//if (!mIsSingleChoice) {
						mWtParams.mDialogInterface.dismiss();
					//}
				}
			});
		}else if (mWtParams.mOnCheckboxClickListener != null) {
			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
					if (mWtParams.mCheckedItems != null) {
						mWtParams.mCheckedItems[position] = listView.isItemChecked(position);
					}
					mWtParams.mOnCheckboxClickListener.onClick(mWtParams.mDialogInterface, position, listView.isItemChecked(position));
				}
			});		
		}
		
    		if (mWtParams.mOnItemSelectedListener != null) {
                listView.setOnItemSelectedListener(mWtParams.mOnItemSelectedListener);
            }

		if (mWtParams.mIsMultiChoice) {
			listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		}
    		mWtParams.mListView = listView;
    	}
    }
    
    public static class RecycleListView extends ListView {
        boolean mRecycleOnMeasure = true;

        public RecycleListView(Context context) {
            super(context);
        }

        public RecycleListView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public RecycleListView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        @Override
        protected boolean recycleOnMeasure() {
            return mRecycleOnMeasure;
        }
    }
}
